package main

import "fmt"

// 策略模式定义了一系列的算法，并将每个算法封装起来，使它们可以相互替换。策略模式让算法的变化独立于使用算法的客户端

// Strategy 策略接口
type Strategy interface {
	Execute() string
}

// ConcreteStrategyA 具体策略 A
type ConcreteStrategyA struct{}

func (csa *ConcreteStrategyA) Execute() string {
	return "Executing strategy A"
}

// ConcreteStrategyB 具体策略 B
type ConcreteStrategyB struct{}

func (csb *ConcreteStrategyB) Execute() string {
	return "Executing strategy B"
}

// Context 上下文
type Context struct {
	strategy Strategy
}

func (c *Context) SetStrategy(strategy Strategy) {
	c.strategy = strategy
}

func (c *Context) ExecuteStrategy() string {
	return c.strategy.Execute()
}

func main() {
	context := &Context{}

	strategyA := &ConcreteStrategyA{}
	context.SetStrategy(strategyA)
	fmt.Println(context.ExecuteStrategy())

	strategyB := &ConcreteStrategyB{}
	context.SetStrategy(strategyB)
	fmt.Println(context.ExecuteStrategy())
}
